<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="设备名称" prop="deviceName">
        <el-input v-model="queryParams.deviceName" placeholder="请输入设备名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="日期">
        <el-date-picker v-model="dateRange" value-format="yyyy-MM-dd HH:mm:ss" type="daterange" range-separator="-"
          start-placeholder="开始日期" end-placeholder="结束日期" style="width: 240px"
          :default-time="['00:00:00', '23:59:59']"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['cultivate:yearPlan:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['cultivate:yearPlan:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['cultivate:yearPlan:remove']"
        >删除</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['cultivate:yearPlan:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="printBox"
          v-hasPermi="['cultivate:yearPlan:print']"
        >打印</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table
      @selection-change="handleSelectionChange"
      fit
      v-loading="loading"
      border
      :data="dataList"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" />

      <el-table-column show-overflow-tooltip label="年度">
        <template slot-scope="scope">
          <span>{{ scope.row.year || "--" }}年度实验室人员培训计划</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="状态" prop="statusName" />
      <el-table-column show-overflow-tooltip label="创建时间" prop="createTime" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="240"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            v-hasPermi="['cultivate:yearPlan:info']"
            @click="handleDetail(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['cultivate:yearPlan:edit']"
          >补充信息</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['cultivate:yearPlan:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 打印模板位置 -->
    <PrintBox
      :pageNum="queryParams.pageNum"
      :total="total"
      :printRow="printList"
      :isVertical="false"
      id="printBox"
    >
      <template slot="title" slot-scope="scope">{{ scope.item.year }}年度试验室人员培训计划表 STET/B/14-1</template>
      <template slot-scope="scope">
        <!-- 表格  class  table-page  必须-->
        <table style="border-collapse: collapse" border="1" cellspacing="0" class="table-page">
          <thead>
            <tr class="table_header">
              <th style="width: 20mm">序号</th>
              <th style="width: 30mm">部门</th>
              <th style="width: 45mm">培训内容</th>
              <th style="width: 45mm">实施时间</th>
              <th style="width: 45mm">参加人员</th>
              <th style="width: 50mm">培训形式/主讲人</th>
              <th style="width: 45mm">培训成果</th>
              <th style="width: 35mm">备注</th>
            </tr>
          </thead>

          <!-- 同列表接口数据 -->
          <tbody>
            <tr v-for="(e, i) in 10">
              <td>
                {{
                scope.item.contentList && scope.item.contentList[i]
                ? i + 1
                : ""
                }}
              </td>
              <td>
                {{
                (scope.item.contentList[i] &&
                scope.item.contentList[i].deptName) ||
                ""
                }}
              </td>
              <td>
                {{
                scope.item.contentList[i] && scope.item.contentList[i].content
                }}
              </td>
              <td>
                {{
                scope.item.contentList[i] &&
                scope.item.contentList[i].operTime
                }}
              </td>
              <td>
                <span
                  v-for="user in scope.item.contentList[i] &&
                  scope.item.contentList[i].joinUserList"
                >{{ user.userName }}</span>
              </td>
              <td>
                <span v-if="scope.item.contentList[i]">
                  {{
                  checkDict(
                  dict.type.sys_cultivate_type,
                  scope.item.contentList[i] &&
                  scope.item.contentList[i].terrainType
                  ) || ""
                  }}
                  /{{
                  scope.item.contentList[i] &&
                  scope.item.contentList[i].hostUserName
                  }}
                </span>
              </td>
              <td>
                {{
                scope.item.contentList[i] && scope.item.contentList[i].result
                }}
              </td>
              <td>
                {{
                scope.item.contentList[i] && scope.item.contentList[i].remark
                }}
              </td>
            </tr>
          </tbody>
        </table>
        <!-- 底部信息位置 -->
        <div class="page-bottom" style="page-break-after: always">
          <div style="display: flex; text-align: left">
            <div style="width: 30%">
              计划编制人:
              <template v-if="baseUrl + scope.item.esign">
                <img style="width: 80px; " :src="baseUrl + scope.item.esign" />
              </template>
            </div>
            <div style="width: 30%;display: flex;align-items: center;">
              批准人:
              <template v-if="CheckApprove(scope, 'zjl').ShowImg">
                <img style="width: 80px; " :src="CheckApprove(scope, 'zjl').ApproveImg" />
              </template>
            </div>
            <div
              style="width: 30%"
            >批准日期:{{ scope.item.wfDetailVo.historyProcNodeList[1].commentList.length>0?scope.item.wfDetailVo.historyProcNodeList[1].commentList[0].time.substr(0,11):'' }}</div>
          </div>
          <div class="desc">注：本表格自2021年6月1日起生效</div>
        </div>
      </template>
    </PrintBox>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  listCultivateYearPlan,
  delCultivateYearPlan
} from "@/api/cultivate/yearPlan.js";
import { deptTreeSelect } from "@/api/system/user.js";
import printStyle from "@/utils/print.js";
export default {
  name: "yearPlan",
  dicts: ["sys_cultivate_type"],

  data() {
    return {
      baseUrl: this.$VUE_APP_IMG_API,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      dataList: [],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      deptOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceName: undefined,
        operUserId: undefined,
        licenseNum: undefined,
        checkDate: null
      },
      // 日期范围
      dateRange: [],

      userList: [],
      deptOptions: [],
      infos: [], //勾选的数据
      printList: [],
      list: []
    };
  },
  created() {
    this.getList();
    this.getDeptTree();
  },
  methods: {
    checkApproveImg(e) {
      let msg = e.split("@eSign@") || [];
      let url = this.$VUE_APP_IMG_API + (msg.length == 1 ? msg[0] : msg[1]);
      return url;
    },
    printBox() {
      //infos  勾选的数据 如果有勾选就打印勾选。
      if (this.infos.length) {
        this.printList = this.infos;
      } else {
        //反之打印当前整个列表
        this.printList = this.dataList;
      }
      setTimeout(() => {
        this.$print({
          printable: "printBox", //打印区域id
          type: "html", //打印类型是html
          scanStyles: false,
          targetStyles: ["*"],
          style: this.workStyle()
        });
      }, 500);
    },
    // 可以自定义样式  printStyle 是公共样式 切勿私自修改
    workStyle() {
      return `${printStyle}  
      .desc{
        text-align: left; font-size: 12px; margin-top: 10px
      }
        table,
      table tr th,
      table tr td {
        border: 1px solid;
        height: 35px;
        padding: 10px;
        
      }
      tr {
        page-break-inside: avoid;
      }
      .table_header{
        font-size:16px;
      }
      `;
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.cultivateYearPlanId);
      this.infos = selection;
      // this.infos = [
      //   {
      //     contentList: selection,
      //   },
      // ];

      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      const configId = row.cultivateYearPlanId || this.ids;
      this.$router.push(
        "./addYearPlan?isEdit=true&id=" +
          configId +
          "&procInsId=" +
          row.procInsId +
          "&statusName=" +
          row.statusName +
          "&taskId=" +
          row.taskId
      );
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },
    handleDetail(row) {
      this.$router.push(
        "./addYearPlan?isInfo=true&id=" + row.cultivateYearPlanId
      );
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const configId = row.cultivateYearPlanId || this.ids;
      let name = this.checkRow(
        configId,
        this.dataList,
        "cultivateYearPlanId",
        "DELETE",
        "year"
      );
      this.$modal
        .confirm('是否确认删除年度为"' + name + '"的数据项？')
        .then(function() {
          return delCultivateYearPlan(configId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    handleAdd() {
      this.$router.push("./addYearPlan");
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      listCultivateYearPlan(
        this.addDateRangeTime(
          this.queryParams,
          this.dateRange,
          "startEndDate",
          "endEndDate"
        )
      ).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleExport() {
      this.download(
        "system/cultivateYearPlan/export",
        {
          ...this.queryParams
        },
        `年度实验室人员培训计划记录表_${this.parseTime(
          new Date().getTime(),
          "{y}-{m}-{d}"
        )}.xlsx`
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    }
  }
};
</script>
<style lang="scss" scoped>
::v-deep .el-dialog__body {
  img {
    width: 100%;
  }
}
</style>
